package com.hj.aiminai.config;

import com.hj.aiminai.docment.Msg;
import com.hj.aiminai.repository.MsgReposttory;
import com.hj.aiminai.service.MsgService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class ChatStorageMemory implements ChatMemory {

    private final MsgReposttory msgReposttory;

    private final MsgService msgService;

    @Override
    public void add(String conversationId, List<Message> messages) {
        for (Message message:messages
             ) {
            Msg msg = new Msg(conversationId, message);
            log.info("add msg, type {}, text{}",message.getMessageType(),msg.getText());
            msgReposttory.save(msg);
        }
    }

    @Override
    public List<Message> get(String conversationId) {
        //根据类型筛选出对应消息文本
        return msgService.findByConversationId(conversationId).stream().map(Msg::toMessage).toList();
    }

    @Override
    public void clear(String conversationId) {

    }
}
